#ifndef __CLIENT_HPP
#define __CLIENT_HPP

#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<string>
#include<stdlib.h>
#include<functional>
#include<unistd.h>
#include"Protocol.hpp"
#include<sys/wait.h>
#define NUM 5
#endif

class Client{
    private:
      std::string _ip;
      int _port;
      int _sock;
    public:
      Client(std::string ip,int port)
        :_ip(ip),
         _port(port),
        _sock(-1)
      {}
      void ClientInit()
      {
           _sock = socket(AF_INET, SOCK_STREAM,0 );
      }
      void service(int sock)
      {
           /*短连接*/  
           struct request requ;
        
           recv( sock , &requ, sizeof(requ), 0 );
            
           struct response resp; 
           resp.code = 0; //默认为0
           
           send( sock, &resp , sizeof(resp), 0 );
      }
      void start()
      {
          struct sockaddr_in local;
          socklen_t len = sizeof(local);
          local.sin_family = AF_INET; 
          local.sin_port = htons(_port);
          local.sin_addr.s_addr = inet_addr(_ip.c_str());
          if(connect(_sock,(struct sockaddr*)&local , sizeof(local) ) < 0 )
          {
              std::cerr<<" connect error "<<std::endl;
              exit(4);
          }
          struct request requ;
          std::cout <<"x:";fflush(stdout);
          std::cin>>requ.x;
          std::cout<<"op:";fflush(stdout);
          std::cin>>requ.op;
          std::cout<<"y:";fflush(stdout);
          std::cin>>requ.y;

          send(_sock , &requ, sizeof(requ),0);

          std::cout<<"#############"<<std::endl;
          struct response resp;
          recv(_sock , &resp, sizeof(resp),0);

          if(resp.code != 0 )
          {
             std::cout<<"exit code is "<<resp.code<<std::endl;
          }
          else std::cout<<"result :"<<resp.result<<std::endl;
      }
      ~Client()
      {
         close(_sock);
      }
};
